package com.demo.order_service.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.order_service.dao.OrderDao;
import com.demo.order_service.enums.ResultEnum;
import com.demo.order_service.model.entity.Order;
import com.demo.order_service.model.result.ResultEntity;
import com.demo.order_service.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/** * @author 付嘉星 
/** * ٩(*Ӧ)و写的全对
/** * 困到昏厥(✽ ﾟдﾟ ✽)  
/** * @date 创建时间：2018年4月19日 下午1:54:04 
/** * @version 1.0 */

/**
 * 描述：通过调用dao或远程服务接口来完成订单管理业务，为调用者提供
 * 订单管理相关的业务方法。
 */

@Service
public class OrderServiceImpl implements OrderService {
	//constant: 每页显示记录数
	 private static final int PAGE_SIZE = 10;
	//订单的DAO对象
	    private final OrderDao orderDao;
	    
	    /**
	     * 构造函数
	     *
	     * @param orderDao
	     */
		 public OrderServiceImpl(@Autowired OrderDao orderDao) {
	        this.orderDao = orderDao;
	    }
		
	@Override
	public PageInfo getOrderByPage(int pageNum) {
		return PageHelper
                .startPage(pageNum, PAGE_SIZE)
                .doSelectPageInfo(orderDao::selectAll);
	}

	
	@Override
	public Order getOrderById(Long oid) {
		// TODO 自动生成的方法存根
		 return orderDao.selectById(oid);
	}

	
	@Override
	public List<Order> getAllOrders() {
		// TODO 自动生成的方法存根
		return orderDao.selectAll();
	}

	
	@Override
    public ResultEntity insertOrder(Order order) {
		// TODO 自动生成的方法存根	
		int dltCnt = -1;
        //处理结果实体
        ResultEntity resultEntity = null;

        try {
            dltCnt = orderDao.insertOrder(order);
        } catch (Exception e) {
            resultEntity = new ResultEntity(ResultEnum.EXC_DB, "添加失败，数据库异常");
            return resultEntity;
        }

        if (dltCnt == 0) {
            resultEntity = new ResultEntity(ResultEnum.ORDER_STATUS_FAIL_CHECK,"添加订单失败" );
        } else {
            resultEntity = new ResultEntity(ResultEnum.ORDER_STATUS_SUCCESS,"添加订单成功");
        }

        return resultEntity;
	}


	
	@Override
	public ResultEntity deleteOrder(Long id) {
		//删除数量
        int dltCnt = -1;
        //处理结果实体
        ResultEntity resultEntity = null;

        try {
            dltCnt = orderDao.deleteById(id);
        } catch (Exception e) {
            resultEntity = new ResultEntity(ResultEnum.EXC_DB, "删除失败，数据库异常");
            return resultEntity;
        }

        if (dltCnt == 0) {
            resultEntity = new ResultEntity(ResultEnum.ORDER_STATUS_FAIL_CHECK,"对应数据不存在，id = " + id);
        } else {
            resultEntity = new ResultEntity(ResultEnum.ORDER_STATUS_SUCCESS,"删除数据id = " + id);
        }

        return resultEntity;
	}

	
	@Override
	public ResultEntity updateById(Long oid ,Order order) {
		// TODO 自动生成的方法存根
	int dltCnt = -1;
    //处理结果实体
    ResultEntity resultEntity = null;

    try {
    	order.setOid(oid);
        dltCnt = orderDao.updateById(order);
    } catch (Exception e) {
        resultEntity = new ResultEntity(ResultEnum.EXC_DB, "修改失败，数据库异常");
        return resultEntity;
    }

    if (dltCnt == 0) {
        resultEntity = new ResultEntity(ResultEnum.ORDER_STATUS_FAIL_CHECK,"对应数据不存在，id = " + oid);
    } else {
        resultEntity = new ResultEntity(ResultEnum.ORDER_STATUS_SUCCESS,"修改订单状态数据id = " + oid);
    }
    return resultEntity;
}
}
